package com.brush.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brush.common.constant.CacheConstants;
import com.brush.common.core.domain.entity.SysDictData;
import com.brush.common.core.domain.resp.SysDictDataQueryResp;
import com.brush.common.exception.database.RecordNotFoundException;
import com.brush.system.domain.req.sysdictdata.SysDictDataAddReq;
import com.brush.system.domain.req.sysdictdata.SysDictDataEditReq;
import com.brush.system.mapper.SysDictDataMapper;
import com.brush.system.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author 18494
* @description 针对表【sys_dict_data(字典数据表)】的数据库操作Service实现
* @createDate 2024-08-19 10:28:03
*/
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData>
    implements SysDictDataService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询所有启用字典数据
     *
     * @return
     */
    @Override
    public List<SysDictDataQueryResp> findEnableAll() {
        return this.baseMapper.findEnableAll();
    }

    @Override
    public Integer countDictDataByType(String dictType) {
        LambdaQueryWrapper<SysDictData> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictData::getDictType,dictType);
        return this.baseMapper.selectCount(queryWrapper).intValue();
    }

    @Override
    public SysDictDataQueryResp selectDictDataById(Long dictCode) {
        return this.baseMapper.selectDictDataById(dictCode);
    }

    @Override
    public boolean insertDictData(SysDictDataAddReq req) {
        SysDictData data=new SysDictData();
        BeanUtil.copyProperties(req, data);
        boolean result =this.save(data);
        if (result) {
            List<SysDictDataQueryResp> dictDatas = this.baseMapper.selectDictDataByType(data.getDictType());
            String redisKey = CacheConstants.SYS_DICT_KEY + req.getDictType();
            redisTemplate.opsForValue().set(redisKey, dictDatas);
        }
        return result;
    }

    @Override
    public boolean updateDictData(SysDictDataEditReq req) {
       SysDictData dbData= this.getById(req.getDictCode());
       if(Objects.isNull(dbData)){
           throw new RecordNotFoundException();
       }

        BeanUtil.copyProperties(req,dbData);
        boolean result = this.updateById(dbData);
        if (result) {
            List<SysDictDataQueryResp> dictDatas = this.baseMapper.selectDictDataByType(dbData.getDictType());
            String redisKey = CacheConstants.SYS_DICT_KEY + req.getDictType();
            redisTemplate.opsForValue().set(redisKey, dictDatas);
        }
        return result;
    }

    @Override
    public void deleteDictDataByIds(Long[] dictCodes) {
        for (Long dictCode : dictCodes) {
            SysDictDataQueryResp resp = this.selectDictDataById(dictCode);
            this.removeById(dictCode);
            List<SysDictDataQueryResp> dictDatas = this.baseMapper.selectDictDataByType(resp.getDictType());
            String redisKey = CacheConstants.SYS_DICT_KEY + resp.getDictType();

            redisTemplate.opsForValue().set(redisKey, dictDatas);
        }
    }


}




